package com.atguigu.gmall.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.atguigu.gmall.entity.PerHourDAU;
import com.atguigu.gmall.entity.PerHourGVM;
import com.atguigu.gmall.service.PublisherService;
import com.atguigu.gmall.service.PublisherServiceImpl;
import com.google.gson.JsonObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Date:2021/3/19
 * Author:csw
 * Description:
 */
@RestController
public class PublisherController {

@Autowired
private PublisherServiceImpl psImp;

    @RequestMapping(value = "/realtime-total")
    public List handle(String date){
        Integer dauByDateCount = psImp.getDAUByDate(date);
        ArrayList arrCell = new ArrayList();
        Map<String, String> newDau = new HashMap<String, String>();
        if(dauByDateCount==null){
            dauByDateCount=0;
        }
        newDau.put("id","dau");
        newDau.put("name","新增日活");
        newDau.put("value",dauByDateCount.toString());

        Integer newMidCount = psImp.getNewMidByDate(date);
        Map<String, String> newMid = new HashMap<String, String>();
        if(newMid==null){
            newMidCount=0;
        }
        newMid.put("id","new_mid");
        newMid.put("name","新增设备");
        newMid.put("value",newMidCount.toString());

        Double dateGVM = psImp.getDateGVM(date);
        Map<String,String> newMoney=new HashMap<String,String>();
        if(dateGVM==null){
            dateGVM=0.0;
        }
        newMoney.put("id","order_amount");
        newMoney.put("name","新增交易额");
        newMoney.put("value",dateGVM.toString());

        arrCell.add(newDau);
        arrCell.add(newMid);
        arrCell.add(newMoney);

        return arrCell;
    }

    /*
    {"yesterday":{"11":383,"12":123,"17":88,"19":200 },
        "today":{"12":38,"13":1233,"17":123,"19":688 }}
     */
    @RequestMapping(value = "/realtime-hours")
    public JSONObject handleHour(String id,String date){
        if("dau".equals(id)){
            return dauhandle(date);
        }
        if("order_amount".equals(id)){
            return gvmhandle(date);
        }
        return null;
    }

    public JSONObject dauhandle(String date){
        LocalDate todayDatas = LocalDate.parse(date);
        LocalDate yesDatas = todayDatas.minusDays(1);

        //    List<PerHourDAU> todayDatas = psImp.getPerByHour(date);

        List<PerHourDAU> todayList = psImp.getPerByHour(todayDatas.toString());
        List<PerHourDAU> yesList = psImp.getPerByHour(yesDatas.toString());

        JSONObject todayJson = parseListPerHourDAU(todayList);
        JSONObject yesdayJSon = parseListPerHourDAU(yesList);

        JSONObject result = new JSONObject();
        result.put("yesterday",yesdayJSon);
        result.put("today",todayJson);
        return result;
    }
    public JSONObject gvmhandle(String date){

        List<PerHourGVM> todayHourGVM = psImp.getHourGVM(date);
        JSONObject result = new JSONObject();
        JSONObject todayData = parseListPerHourGVM(todayHourGVM);

        LocalDate parse = LocalDate.parse(date);
        LocalDate yesterday = parse.minusDays(1);

        List<PerHourGVM> yesterdayHourGVM = psImp.getHourGVM(yesterday.toString());
        JSONObject yesDate = parseListPerHourGVM(yesterdayHourGVM);
        result.put("yesterday",yesDate);
        result.put("today",todayData);
        return result;
    }




        public JSONObject parseListPerHourDAU(List<PerHourDAU> list){

            JSONObject result = new JSONObject();
            for (PerHourDAU perHourDAU : list) {
                result.put(perHourDAU.getHour(),perHourDAU.getDaucount());
            }
            return  result;
        }

    public JSONObject parseListPerHourGVM(List<PerHourGVM> list){

        JSONObject result = new JSONObject();
        for (PerHourGVM perHourGVM : list) {
            result.put(perHourGVM.getHour(),perHourGVM.getSumMoney());
        }
        return  result;
    }
}
